package revontuli.aMHD;

import java.util.List;

import revontuli.aMHD.R;
import revontuli.aMHD.R.id;
import revontuli.aMHD.R.layout;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.widget.Toast;

public class StopMapAct extends MapActivity {
	// pripojenie na GPS sluzbu
	private UserLocationSvc mService = null;
	private Messenger mReceiver = new Messenger(new IncomingHandler());
	public Handler mHandler;
	
//	private Button mListButton;
	
	MapView mMapView;
	
	// spravca objektov mapy
	private List<Overlay> mapOverlays;
	
	// znacka mojej polohy
	//TODO dorobit vlastny overlay
	private MyLocationOverlay mMyLocOverlay;
	
	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
			UserLocationSvc.ClientBinder binder = (UserLocationSvc.ClientBinder) service;
			mService = binder.getService();
			mService.registerMessenger(mReceiver);
		}

		public void onServiceDisconnected(ComponentName arg0) {
			mService = null;
		}
	};
	
	class IncomingHandler extends Handler {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case aMHDApp.MSG_REFRESH_GPS: // pozicia bola obnovena
				if (mService != null) {
					updateMapUI();
				}
				break;
			}
		}
	};
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.stopmap);

//		mListButton = (Button)findViewById(R.id.listButton);
//		this.mListButton.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				Intent in1 = new Intent(v.getContext(), BusStopFinder.class);
//				startActivity(in1);
//			}
//		});

		// inicializacia mapy
		mMapView = (MapView) findViewById(R.id.mapview);
		mMapView.setBuiltInZoomControls(true);
		mMapView.setSatellite(false);
		
		mapOverlays = mMapView.getOverlays();

		
//		MapItemizedOverlay mItemizedOverlay = new MapItemizedOverlay(getResources().getDrawable(R.drawable.my_arrow));
//		OverlayItem mMyLocOverlayItem = new OverlayItem(getGeoPoint(mService.getLocation()), "text1", "text2");
//		mItemizedOverlay.addOverlay(mMyLocOverlayItem);
//		mapOverlays.add(mItemizedOverlay);
		
		// znacka mojej polohy
		mMyLocOverlay = new MyLocationOverlay(this, mMapView);
		mapOverlays.add(mMyLocOverlay);
	}
	
	@Override
	public void onStart() {
		super.onStart();
		if (aMHDApp.DEBUG) {
			Toast.makeText(getApplicationContext(), "map bind", Toast.LENGTH_SHORT).show();
		}
		
		//pripojenie aktivity k GPS sluzbe
		Intent intent = new Intent(this, UserLocationSvc.class);
		bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
		
	}
	
	@Override
	public void onResume() {
		super.onResume();
		updateMapUI();
		mMyLocOverlay.enableMyLocation();
		mMyLocOverlay.enableCompass();
	}
	
	@Override
	public void onPause() {
		super.onPause();
		
		mMyLocOverlay.disableMyLocation();
		mMyLocOverlay.disableCompass();
	}
	
	@Override
	public void onStop() {
		super.onStop();
		if (mService != null) {
			unbindService(mConnection);
			if (aMHDApp.DEBUG) {
				Toast.makeText(getApplicationContext(), "map unbind", Toast.LENGTH_SHORT).show();
			}
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	private void updateMapUI() {
//		getGeoPoint(mService.getLocation());
		
		
	}
	
	private GeoPoint getGeoPoint(Location loc) {
		Double lat = loc.getLatitude()*1E6;
		Double lng = loc.getLongitude()*1E6;
		return new GeoPoint(lat.intValue() ,lng.intValue());
	}
}